//解密文件
import java.io.*;
public class Demo0301 {
    public static void main(String[] args) {
        System.out.println("#解密文件#");
        System.out.print("源文件路径：");
        String SrcFileName = inputFileName();
        String DestFileName = "C:\\Users\\98317\\Desktop\\JavaExerciseCode\\exci07\\解密文件.txt";
        OpenLock(SrcFileName, DestFileName);
    }
    //获取输入的文件名
    private static String inputFileName() {
        String fileName = null;
        //System.in是Stream，InputStreamReader把字节流格式化为字符流读取，以字符流的形式读取输入缓冲区
        BufferedReader buffRead = new BufferedReader(new InputStreamReader(System.in));
        try {
            fileName = buffRead.readLine();  //得到字符串形式的文件路径
        } catch (IOException error) {   //抛出异常
            error.printStackTrace();
        }
        return fileName;
    }
    //解密
    public static void OpenLock(String SrcfileName, String DestfileName) {
        try {
            BufferedInputStream SrcFile = new BufferedInputStream(new FileInputStream(SrcfileName));
            BufferedOutputStream DestFile = new BufferedOutputStream(new FileOutputStream(DestfileName));
            int key = SrcFile.read();
            if (key < 0) {
                System.out.println("密钥有误！");
            } else {
                for (int i; (i = SrcFile.read()) >= 0; )
                    DestFile.write(i^key);
                System.out.println("解密成功！");
            }
            DestFile.close();
            SrcFile.close();
        } catch (IOException error) {
            error.printStackTrace();
        }
    }
}
